//
//  KFPointUtil.m
//  Imp
//
//  Created by Alex Carter on 15/06/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "KFPointUtil.h"


KFFloat KFPointUtil::crossProduct(KFPoint p1,KFPoint p2) 
{
    return (p1.x * p2.y - p2.x * p1.y); 
}

KFFloat KFPointUtil::dotProduct(KFPoint p1,KFPoint p2)
{
    return (p1.x * p2.x + p1.y * p2.y); 
}

KFFloat KFPointUtil::length(KFPoint p)
{	
    return sqrt(p.x * p.x + p.y * p.y); 
}

KFFloat KFPointUtil::squareLength(KFPoint p) 
{	
    return (p.x * p.x + p.y * p.y); 
}

KFPoint KFPointUtil::add(KFPoint a,KFPoint b)
{
    return KFPointMake(a.x + b.x,a.y + b.y); 
}

KFPoint KFPointUtil::sub(KFPoint a,KFPoint b) 
{
    return KFPointMake(a.x - b.x,a.y - b.y); 
}

KFPoint KFPointUtil::mul(KFPoint p,KFFloat mag) 
{
    return KFPointMake(p.x * mag, p.y * mag); 
}

KFPoint KFPointUtil::inverse(KFPoint p)
{
    return KFPointMake(0.0 - p.x,0.0 - p.y); 
}

KFPoint KFPointUtil::unit(KFPoint p)  
{ 
    KFFloat len = length(p);
    return KFPointMake(p.x / len, p.y / len); 
}

float KFPointUtil::measureAngle(KFPoint a,KFPoint b)
{
    float magA = length(a);
    float magB = length(b);
#define MIN_MAGNITUDE_FOR_MEASURE_ANGLE 0.001
    if(magA < MIN_MAGNITUDE_FOR_MEASURE_ANGLE || magB < MIN_MAGNITUDE_FOR_MEASURE_ANGLE)
    {
        return 0.0;
    }
    
    return asin(crossProduct(a,b) / (magA * magB));
}

